// 二次封装axios
import axios from "axios";
// 导入nprogress
import nprogress from 'nprogress'
// 引入进度条样式
import 'nprogress/nprogress.css'
import {ElMessage} from "element-plus";


// 创建一个axios实例
const requests = axios.create({
    baseURL: '/api',
    timeout: 5000,
});


//请求拦截器
requests.interceptors.request.use((config) => {
    if (localStorage.getItem('UUIDTOKEN')) {
        config.headers.userTempId = localStorage.getItem('UUIDTOKEN')
        // console.log(config.headers.userTempId)
    }
    if (localStorage.getItem('TOKEN')) {
        config.headers.token = localStorage.getItem('TOKEN')
    }
    nprogress.start()
    return config;
});


//响应拦截器
requests.interceptors.response.use((res) => {
    nprogress.done()
    return res.data
}, () => {
    ElMessage({type: 'error', message: '服务器响应数据失败,请联系管理员!', center: 'true'})
})


// 对外暴露
export default requests;
